<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入ID" clearable type="number"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" @keyup.enter.native="handleQuery" placeholder="审核状态">
          <el-option v-for="(item, index) in applyWithDreawal" :key="index" :label="item" :value="index"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="提现状态" prop="amountStatus">
        <el-select v-model="queryParams.amountStatus" @keyup.enter.native="handleQuery" placeholder="提现状态">
          <el-option v-for="(item, index) in withDrawStatus" :key="index" :label="item" :value="index"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="更新日期" prop="dateRange">
        <el-date-picker v-model="dateRange" style="width: 320px" type="datetimerange" align="right"
          start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"
          value-format="timestamp"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['withdrawal:withdrawal:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="提现金额" align="center" prop="amount" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ applyWithDreawal[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提现状态" align="center" prop="amountStatus">

        <template slot-scope="scope">
          <span>{{ withDrawStatus[scope.row.amountStatus] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applyTime">

        <template slot-scope="scope">
          <span>{{ scope.row.applyTime | timestampToDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 0" size="mini" type="text" icon="el-icon-edit"
            @click="applyStatus(scope.$index, scope.row)" v-hasPermi="['withdrawal:withdrawal:apply']">审核</el-button>
          <el-button v-if="scope.row.status == 2 && scope.row.amountStatus == 0" size="mini" type="text"
            icon="el-icon-edit" @click="changePayStatus(scope.$index, scope.row)"
            v-hasPermi="['withdrawal:withdrawal:updateAmountStatus']">变更状态</el-button>
          <el-button size="mini" type="text" icon="el-icon-record" @click="handleRecord(scope.$index, scope.row)"
            v-hasPermi="['withdrawal:withdrawal:record']">提现记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <apply :visiable="applyVisible" :Obj="row" title="审核" column="status" disableColumn="amount"
      requestURL="/withdrawal/withdrawal/apply" @close="closeApply"></apply>

    <pay :visiable="visible" :Obj="row" title="更新支付状态" column="amountStatus" disableColumn="amount"
      requestURL="/withdrawal/withdrawal/updateAmountStatus" @close="closePay"></pay>
  </div>
</template>

<script>
import { listWithdrawal, getWithdrawal, delWithdrawal, addWithdrawal, updateWithdrawal } from "@/api/finance/withdrawal";
import { APPLY_WITHDRAW_LABEL, WITHDRAW_STATUS } from '@/constant/const';
import { timestampToDate } from "@/utils/time"
export default {
  name: "Withdrawal",
  components: {
    apply: () => import("./component/apply.vue"),
    pay: () => import("./component/dialogForm.vue"),
  },
  data() {
    return {
      dateRange:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 打款 配置
      visible: false,
      // 审核配置
      applyVisible: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      selectIndex: 0,
      // 提现管理表格数据
      withdrawalList: [],
      // 弹出层标题
      title: "",

      row: {},
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null,
        amount: null,
        status: null,
        amountStatus: null,
        applyTime: null,
        email:null,
        phone:null,
        params:{
          startTime:null,
          endTime:null
        }
      },
      applyWithDreawal: APPLY_WITHDRAW_LABEL,
      withDrawStatus: WITHDRAW_STATUS,
      isSearch: false
    };
  },
  created() {
    this.getList();
  },
  filters: {
    timestampToDate
  },
  methods: {

    /**
     * 审核
     */
    applyStatus(index, row) {
      this.selectIndex = index;
      this.row = row;
      this.applyVisible = true;
    },

    changePayStatus(index, row) {
      this.selectIndex = index;
      this.row = row;
      this.visible = true;
    },

    /**
     * 关闭审核
     * @param {{}} formData 
     * @param {Boolean} value 
     */
    closeApply(formData, value, submit = false) {
      this.applyVisible = value;

      if (!formData.status) {
        return;
      }
      if(!submit){
        return;
      }

      this.withdrawalList[this.selectIndex].status = formData.status;
    },

    closePay(formData, value, submit = false) {
      this.visible = value;
      console.log("formData.amountStatus", formData.amountStatus, "this.selectIndex", this.selectIndex);
      if (!formData.amountStatus) {
        return;
      }
      if(!submit){
        return;
      }
      this.withdrawalList[this.selectIndex].amountStatus = formData.amountStatus;
    },

    /** 查询提现管理列表 */
    getList() {

      let params = {}

      if (this.isSearch) {
        params = Object.assign({}, this.queryParams);
        params['params']['startTime'] = (this.dateRange && this.dateRange.length) ? this.dateRange[0] / 1000 : '';
        params['params']['endTime'] = (this.dateRange && this.dateRange.length) ? this.dateRange[1] / 1000 : '';
      }else{
        params = {
            pageNum: this.queryParams.pageNum,
            pageSize: this.queryParams.pageSize
        }
      }
      this.loading = true;
      listWithdrawal(params).then(response => {
        this.withdrawalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        amount: null,
        status: null,
        amountStatus: null,
        applyTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.isSearch = true;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");

      this.dateRange = undefined
      this.queryParams.pageNum = 1;
      this.isSearch = false;
      this.dateRange = [];
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加提现管理";
    },

    /** 提现记录 */
    handleRecord(index, row) {

      console.log("row:", row);

      sessionStorage.setItem("withdrawalRow", JSON.stringify(row));

      this.$router.push({
        path: `/finance/withdrawal/record/${row.id}`
      });

    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids = this.ids;
      this.download('withdrawal/withdrawal/export', {
        ...this.queryParams,
        ids: this.ids.length? this.ids : undefined
      }, `withdrawal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>